package com.hao.tool.util.util;

import cn.hutool.core.io.FileUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
 * description
 *
 * @author RuiHaoZ on CMHK.jx
 * @date 2020-07-14 22:43
 */
public class FileBlankUtil {

    private static Logger logger = LoggerFactory.getLogger(FileBlankUtil.class);


    public static long fileSizeIsBig(File thisFile, long maxMbSize) throws Exception {
        long fileSize = FileUtil.size(thisFile);
        if (maxMbSize > 0 && fileSize > maxMbSize) {
            throw new Exception("文件过大，不允许该类型操作[" + fileSize + "]");
        }
        return fileSize;
    }

    public static String buildFilePath(String filePath, String absFilePath) {
        String thisFilePath = "";
        if (StringUtils.isNotBlank(absFilePath)) {
            thisFilePath = absFilePath;
        } else {
            thisFilePath = filePath;
        }
        if (StringUtils.isBlank(thisFilePath)) {
            return thisFilePath;
        }
        thisFilePath = thisFilePath.replace("~", File.separator);
        return thisFilePath;
    }

    public static File buildAndFileIsExist(String filePath, String absFilePath) throws Exception {
        logger.info("filePath=[{}],absFilePath=[{}]", filePath, absFilePath);
        String thisFilePath = buildFilePath(filePath, absFilePath);

        if (StringUtils.isBlank(thisFilePath)) {
            throw new Exception("文件路径不存在，请确认参数是否正常");
        }
        File thisFile = new File(thisFilePath);
        if (!thisFile.exists()) {
            throw new Exception("文件不存在，请确认路径是否正确");
        }
        if (!thisFile.isFile()) {
            throw new Exception("该路径不是标准文件，请确认路径是否正确");
        }
        return thisFile;
    }
}
